package Utils
{	
	import Utils.Key;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import Players.Player;

	public class Camera
	{
		public var x:Number = 0;
		public var y:Number = 0;
		
		public var width:int;
		public var height:int;
		
		private var realHeight:int;
		private var realWidth:int;
		
		private var columns:int;
		private var rows:int;
		
		private var bufferBD:BitmapData;
		private var bufferRect:Rectangle;
		
		private var game:Game;
		private var tileSize:int;
		
		public function get Rect():Rectangle {return new Rectangle(this.x - this.tileSize, this.y - this.tileSize, this.width + this.tileSize, this.height + this.tileSize);}
		public function get X():Number {return this.x;}
		public function get Y():Number {return this.y;}
		public function Camera(game:Game, width:int, height:int, tileSize:int)
		{	
			this.game = game;
			this.realHeight = height;
			this.realWidth = width;
			this.height = height + tileSize ;
			this.width = width + tileSize;
			this.tileSize = tileSize;

			this.rows = width/tileSize;
			this.columns = height/tileSize;
			
			bufferBD = new BitmapData(width, height, true, 0x000000);
			bufferRect = new Rectangle(0,0, width, height);
		}
		
		public function Bounds():void
		{
			var hero:Player = game.Hero;
			this.x = hero.WorldPosition.x - (.5 * width);
			this.y = hero.WorldPosition.y - (.5 * height);
			if (this.x < 0) 
			{
				this.x = 0;
				hero.Position.x = hero.WorldPosition.x + this.x;
			}
			else if ((this.x + this.width) > game.CurrentLevel.Width)
			{
				this.x = game.CurrentLevel.Width - this.width;
				hero.Position.x = hero.WorldPosition.x - this.x;
			}
			else
			{
				hero.Position.x = .5 * this.width;
			}
			
			if (this.y < 0) 
			{
				this.y = 0;
				hero.Position.y = hero.WorldPosition.y + this.y;
			}
			else if ((this.y+this.height) > game.CurrentLevel.Height)
			{
				this.y = game.CurrentLevel.Height - this.height;
				hero.Position.y = hero.WorldPosition.y - this.y;
			}
			else
			{
				hero.Position.y = .5 * this.height;
			}
		}
		
		public function Update(deltaTime:int):void
		{
			Bounds();
			//Draw();
		}
		
		public function Draw():void
		{
			
			var currentRow:int = int(this.x/tileSize);
			var currentColumn:int = int(this.y/tileSize);
			var currentLevel:Level = game.CurrentLevel;
			
			if(currentLevel.World != null && currentLevel.TileSheet != null)
			{
				for(var column:int = 0; column < columns; column++)	
				{
					if(column + currentColumn < currentLevel.Columns)
					{
						for(var row:int = 0; row < rows; row++)
						{
							if(row + currentRow < currentLevel.Rows)
							{
								var tile:int = currentLevel.World[column+currentColumn][row+currentRow];
								var tilePoint:Point = new Point(row * currentLevel.TileSize, column * currentLevel.TileSize);	
								//Find Correct Tile To Display
								var tileRectangle:Rectangle = new Rectangle(int(tile % currentLevel.TileSheet.Row) * currentLevel.TileSize, int(tile / currentLevel.TileSheet.Row) * currentLevel.TileSize, tileSize, tileSize);
								
								bufferBD.copyPixels(currentLevel.TileSheet.bitmapData, tileRectangle, tilePoint);
								bufferRect.x = x % tileSize;
								bufferRect.y = y % tileSize;
								
								game.CanvasBitmapData.copyPixels(bufferBD, bufferRect, new Point(0,0));
							}
						}
					}
				}
			}
		}
	}
}